mod common;
use crate::auth::ck_auth;
use crate::common::*;
use serde_json::json;

#[tokio::test]
async fn test_auth_1() {
    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "auth",
        "list",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "auth1",
                "name": "name_auth1",
            }
        }),
        "auth",
        "add",
    )
    .await;

    ck_auth(
        pos!(),
        json!({
            "all": ["auth1"],
            "auth1": {"name": "name_auth1", "sn": "auth1"},
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "auth",
        "list",
        json!([{"name": "name_auth1", "sn": "auth1"}]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "get",
        json!({"name": "name_auth1", "sn": "auth1"}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "rm",
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "auth",
        "list",
        e_node(),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "get",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "rm",
    )
    .await;

    ck_auth(pos!(), json!({})).await;
}

#[tokio::test]
async fn test_auth_2() {
    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "auth1",
                "name": "name_auth1",
            }
        }),
        "auth",
        "add",
    )
    .await;

    ck_auth(
        pos!(),
        json!({
            "all": ["auth1"],
            "auth1": {"name": "name_auth1", "sn": "auth1"},
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "auth2",
                "name": "name_auth2",
            }
        }),
        "auth",
        "add",
    )
    .await;

    ck_auth(
        pos!(),
        json!({
            "all": ["auth1", "auth2"],
            "auth1": {"name": "name_auth1", "sn": "auth1"},
            "auth2": {"name": "name_auth2", "sn": "auth2"},
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "auth",
        "list",
        json!([
                {"sn": "auth1", "name": "name_auth1"},
                {"sn": "auth2", "name": "name_auth2"},
        ]),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": [{
                "sn": "auth1",
                "name": "n_auth1",
            }]
        }),
        "auth",
        "md",
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "auth",
        "list",
        json!([
                {"sn": "auth1", "name": "n_auth1"},
                {"sn": "auth2", "name": "name_auth2"},
        ]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "get",
        json!({"sn": "auth1", "name": "n_auth1"}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "rm",
    )
    .await;

    ck_auth(
        pos!(),
        json!({
            "all": ["auth2"],
            "auth2": {"name": "name_auth2", "sn": "auth2"},
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "auth2",
        }),
        "auth",
        "rm",
    )
    .await;

    ck_auth(pos!(), json!({})).await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "auth",
        "list",
        e_node(),
    )
    .await;
}

#[tokio::test]
async fn test_auth_4() {
    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "auth1",
                "name": "name_auth1",
            }
        }),
        "auth",
        "add",
    )
    .await;

    ck_auth(
        pos!(),
        json!({
            "all": ["auth1"],
            "auth1": {"name": "name_auth1", "sn": "auth1"},
        }),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "getuser",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
            "sn": "1001",
        }),
        "auth",
        "rmuser",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
            "v": ["1001", "1002"]
        }),
        "auth",
        "adduser",
    )
    .await;

    ck_auth(
        pos!(),
        json!({
            "all": ["auth1"],
            "auth1": {"name": "name_auth1", "sn": "auth1"},
            "user1": ["1001", "1002"],
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "getuser",
        json!(["1001", "1002"]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "getuser",
        json!(["1001", "1002"]),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
            "sn": "1001",
        }),
        "auth",
        "rmuser",
    )
    .await;

    ck_auth(
        pos!(),
        json!({
            "all": ["auth1"],
            "auth1": {"name": "name_auth1", "sn": "auth1"},
            "user1": ["1002"],
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
            "sn": "1002",
        }),
        "auth",
        "rmuser",
    )
    .await;

    ck_auth(
        pos!(),
        json!({
            "all": ["auth1"],
            "auth1": {"name": "name_auth1", "sn": "auth1"},
        }),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "getuser",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
            "v": ["1001"]
        }),
        "auth",
        "adduser",
    )
    .await;

    ck_auth(
        pos!(),
        json!({
            "all": ["auth1"],
            "auth1": {"name": "name_auth1", "sn": "auth1"},
            "user1": ["1001"],
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "getuser",
        json!(["1001"]),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
            "sn": "1001",
        }),
        "auth",
        "rmuser",
    )
    .await;

    ck_auth(
        pos!(),
        json!({
            "all": ["auth1"],
            "auth1": {"name": "name_auth1", "sn": "auth1"},
        }),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "getuser",
        e_node(),
    )
    .await;
}

#[tokio::test]
async fn test_auth_5() {
    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "auth1",
                "name": "name_auth1",
            }
        }),
        "auth",
        "add",
    )
    .await;

    ck_auth(
        pos!(),
        json!({
            "all": ["auth1"],
            "auth1": {"name": "name_auth1", "sn": "auth1"},
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
            "v": ["1001", "1002"]
        }),
        "auth",
        "adduser",
    )
    .await;

    ck_auth(
        pos!(),
        json!({
            "all": ["auth1"],
            "auth1": {"name": "name_auth1", "sn": "auth1"},
            "user1": ["1001", "1002"],
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "getuser",
        json!(["1001", "1002"]),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "auth1",
        }),
        "auth",
        "rm",
    )
    .await;

    ck_auth(pos!(), json!({})).await;
}
